package com.aiwu.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiwu.library.bean.ArchiveBean;

import org.citra.citra_emu.R;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 存档配置
 */
public class BakAdapter extends BaseAdapter {
    private List<ArchiveBean> mData;
    private BtnClickListener btnClickListener;

    public BakAdapter() {

    }

    public void setData(List<ArchiveBean> mData) {
        this.mData = mData;
        notifyDataSetChanged();
    }

    public void addData(ArchiveBean itemBean) {
        if (mData == null) {
            mData = new ArrayList<>();
        }
        mData.add(itemBean);
        notifyDataSetChanged();
    }

    public void removeData(int position) {
        if (mData == null) {
            return;
        }
        mData.remove(position);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public ArchiveBean getItem(int position) {
        return mData == null ? null : mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final Context context = parent.getContext();
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_back_up, null);
            holder.tvTime = convertView.findViewById(com.aiwu.R.id.tv_time);
            holder.btnReadArchive = convertView.findViewById(com.aiwu.R.id.btn_read_archive);
            holder.btnDeleteArchive = convertView.findViewById(com.aiwu.R.id.btn_delete_archive);
            convertView.setTag(holder);
        }
        holder = (ViewHolder) convertView.getTag();
        final ArchiveBean item = mData.get(position);
        holder.tvTime.setText(item.getFileTime());
        holder.btnReadArchive.setOnClickListener(v -> {
            if (btnClickListener != null) {
                btnClickListener.readArchiveBtnClick(item.getId(), position);
            }
        });
        holder.btnDeleteArchive.setVisibility(item.getType() == ArchiveBean.NORMAL ? View.VISIBLE : View.GONE);
        holder.btnDeleteArchive.setOnClickListener(v -> {
            if (btnClickListener != null) {
                btnClickListener.deleteArchiveBtnClick(item.getId(), position);
            }
        });
        return convertView;
    }

    public List<ArchiveBean> getData() {
        return mData;
    }

    public void setBtnClickListener(BtnClickListener btnClickListener) {
        this.btnClickListener = btnClickListener;
    }

    static class ViewHolder {
        TextView tvTime;
        Button btnReadArchive;
        Button btnDeleteArchive;
    }

    public interface BtnClickListener {
        void readArchiveBtnClick(int id, int position);

        void deleteArchiveBtnClick(int id, int position);
    }
}
